package com.unicampmaps;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.slidingmenu.lib.app.SlidingFragmentActivity;

public class MenuFragment extends ListFragment {

	private SlidingFragmentActivity activity;

	private SlidingMenuAdapter mAdapter;

	public MenuFragment() {
		/*
		 * http://developer.android.com/reference/android/app/Fragment.html All
		 * subclasses of Fragment must include a public empty constructor. The
		 * framework will often re-instantiate a fragment class when needed, in
		 * particular during state restore, and needs to be able to find this
		 * constructor to instantiate it. If the empty constructor is not
		 * available, a runtime exception will occur in some cases during state
		 * restore.
		 */
	}

	@Override
	public void onResume() {
		super.onResume();
		activity = (SlidingFragmentActivity) this.getActivity();

		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this.activity
						.getApplicationContext());

		// add everything
		mAdapter = new SlidingMenuAdapter(activity);
		mAdapter.addItem(R.string.slidingmenu_home,
				R.drawable.ic_slidingmenu_home);
		if (!prefs.getString("token", "").equals("")) {
			String foursquare_username = prefs.getString("name", "") + " ("
					+ prefs.getString("email", "") + ")";
//			Toast.makeText(this.activity.getApplicationContext(),
//					foursquare_username, Toast.LENGTH_LONG).show();
			// TODO mostrar no item
			mAdapter.addItem(R.string.foursquare_connected,
					R.drawable.foursquare_icon);
		} else {
			mAdapter.addItem(R.string.slidingmenu_foursquare,
					R.drawable.foursquare_icon);
		}

		setListAdapter(mAdapter);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.sliding_menu_list, null);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		SlidingMenuItem item = mAdapter.getItem(position);
		Intent intent;
		// FamiliarFragment fragment = null;
		switch (item.title) {
		case R.string.slidingmenu_home:
			this.activity.toggle();
			break;
		case R.string.slidingmenu_foursquare:
			intent = new Intent(l.getContext(), ActivityWebView.class);
			startActivity(intent);
			break;

		}
		// if (fragment != null) {
		// switchContent(fragment);
		// }
	}

	private class SlidingMenuItem {
		public int title;
		public int icon;
		public boolean isHeader;

		public SlidingMenuItem(int title, int icon, boolean isHeader) {
			this.title = title;
			this.icon = icon;
			this.isHeader = isHeader;
		}
	}

	private class SlidingMenuAdapter extends ArrayAdapter<SlidingMenuItem> {

		public SlidingMenuAdapter(Context context) {
			super(context, 0);
		}

		public void addItem(int title) {
			addItem(title, -1);
		}

		public void addItem(int title, int icon) {
			add(new SlidingMenuItem(title, icon, false));
		}

		@SuppressWarnings("unused")
		public void addHeader(int title) {
			add(new SlidingMenuItem(title, -1, true));
		}

		@Override
		public int getViewTypeCount() {
			return 2;
		}

		@Override
		public int getItemViewType(int position) {
			return getItem(position).isHeader ? 0 : 1;
		}

		@Override
		public boolean isEnabled(int position) {
			return !getItem(position).isHeader;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			SlidingMenuItem item = getItem(position);
			if (convertView == null) {
				int layout = item.isHeader ? R.layout.sliding_menu_header
						: R.layout.sliding_menu_item;
				convertView = LayoutInflater.from(getContext()).inflate(layout,
						null);
			}
			((TextView) convertView.findViewById(R.id.menu_title))
					.setText(item.title);
			ImageView icon = (ImageView) convertView
					.findViewById(R.id.menu_icon);
			if (icon != null) {
				if (item.icon > 0) {
					icon.setVisibility(View.VISIBLE);
					icon.setImageResource(item.icon);
				} else {
					icon.setVisibility(View.GONE);
				}
			}
			return convertView;
		}

	}

}